//
//  ViewController.m
//  HookDemo
//
//  Created by 杨鑫 on 2022/6/29.
//

#import "ViewController.h"
#import "NSObject+Hook.h"

@interface Person : NSObject

@property (nonatomic, copy) NSString *name;

@end

@implementation Person

@end

@interface ViewController ()

@property (nonatomic, strong) Person *person;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    /// 对象先做一次KVO
    /// 然后对KVO后的对象在做一次 isa-swizzling
    /// 这时候修改name，KVO会崩溃
    [self.person addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:nil];
    [self.person hook:@selector(setTip:)];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.person.name = @"123";
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    NSLog(@"%@", change);
}

- (Person *)person
{
    if (!_person) {
        _person = [[Person alloc] init];
    }
    return _person;
}

@end
